//
//  RegularBusViewController.m
//  didacar
//
//  Created by imac on 15/12/1.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "RegularBusViewController.h"
#import "RegularBusCell.h"
#import "BuyTicketHeaderView.h"
#import "SelectLocationViewController.h"
#import "BusLineViewController.h"
#import "RegularBusViewModel.h"
#import "BusLineCell.h"
#import "RoutePlanningViewController.h"
@interface RegularBusViewController () <SelectLocationDelegate>

@property (strong, nonatomic) IBOutlet UITableView *mainTable;
@property (strong, nonatomic) BuyTicketHeaderView *buyTicketHeaderView;

// 按钮的标记
@property (assign, nonatomic) NSInteger btnTag;

@property (strong, nonatomic) PoisModel *startPoisModel;
@property (strong, nonatomic) PoisModel *endPoisModel;

@property (strong, nonatomic) RegularBusViewModel *regularBusViewModel;
@property (strong, nonatomic) IBOutlet UIView *recommendSectionView;

@end

@implementation RegularBusViewController


-(void)awakeFromNib{
    self.regularBusViewModel = [[RegularBusViewModel alloc ] init];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLoginStatusNotificenter];
    

    [self setExtraCellLineHidden:self.mainTable];
    
    self.buyTicketHeaderView = [BuyTicketHeaderView  getInstance];
    self.buyTicketHeaderView.alertStyle = 1;
    self.mainTable.tableHeaderView = self.buyTicketHeaderView;
    self.mainTable.backgroundColor = GLOBLE_BACKGROUND_COLOR;
    
    WEAKSELF
    self.buyTicketHeaderView.BtnBlock = ^void(NSInteger btnTag){
        weakSelf.btnTag = btnTag;
        
        // 出发地或者目的地
        if (btnTag == 1 || btnTag == 2) {
            
            [weakSelf presentSelectLocation];
        }
        // 切换地点
        else if (btnTag == 3){
            PoisModel *tmpModel = weakSelf.endPoisModel;
            
            weakSelf.endPoisModel  = weakSelf.startPoisModel;
            weakSelf.startPoisModel = tmpModel;
            
        }
        // 查询路线
        else if (btnTag == 4){
        
           if ([weakSelf.buyTicketHeaderView validata]){
            
                BusLineViewController *busLineViewController = (BusLineViewController *)[BusLineViewController getViewControllerFromSbName:@"BuyTickets" viewControllerName:@"BusLineViewController"];
                busLineViewController.startPoisModel = weakSelf.startPoisModel;
                busLineViewController.endPoisModel   = weakSelf.endPoisModel;
                [weakSelf.buyTicketsViewController.navigationController pushViewController:busLineViewController animated:YES];
                
                weakSelf.regularBusViewModel.startLng = weakSelf.startPoisModel.longitude;
                weakSelf.regularBusViewModel.startLat = weakSelf.startPoisModel.latitude;
                weakSelf.regularBusViewModel.endLat = weakSelf.endPoisModel.latitude;
                weakSelf.regularBusViewModel.endLng = weakSelf.endPoisModel.longitude;
            }
        }
    };
    
    self.mainTable.mj_footer = [MJDIYBackFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreRecommendData)];
}

-(void)loadNewRecommendData{
    self.regularBusViewModel.pageIndex = 1;
    [self loadData];
}

-(void)loadMoreRecommendData{
    self.regularBusViewModel.pageIndex ++;
    [self loadData];
}

-(void)loadData{
    WEAKSELF
    [self.regularBusViewModel getRegularBusRecommend:^(NSInteger resultType, id data, NSString *errorMsg) {
        
        [weakSelf.mainTable.mj_footer endRefreshing];
        if (resultType == 1|| resultType == 3) {
            
            weakSelf.mainTable.alpha = 1;
            [weakSelf.mainTable reloadData];
            
            if (resultType == 3) {
                weakSelf.mainTable.mj_footer.hidden = YES;
            }else{
                weakSelf.mainTable.mj_footer.hidden = NO;
                [weakSelf loadMoreRecommendData];
            }
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
    }];
}

#pragma mark --------登录状态回掉---------------
-(void)addLoginStatusNotificenter{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:LoginSuccess object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoutSuccess) name:LogoutSuccess object:nil];
}

-(void)removeLoginStatusNotificenter{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)loginSuccess{
    
}

-(void)logoutSuccess{
    
}


-(void)presentSelectLocation{
    UINavigationController *cityNavigation = (UINavigationController *)[BaseViewController getInitialViewControllerFromSbName:@"SelectLocation"];
    SelectLocationViewController *selecetdLocation = cityNavigation.viewControllers[0];
    selecetdLocation.delegate = self;
    
    if (self.btnTag == 1) {
        selecetdLocation.searchBarPlaceholder = @"出发地查询";
    }else{
        selecetdLocation.searchBarPlaceholder = @"目的地查询";
    }
    
    [self.buyTicketsViewController presentViewController:cityNavigation animated:YES completion:^{
        
    }];
}

-(void)selectedPoiModel:(PoisModel *)poisModel{
    if (self.btnTag == 1) {
        self.startPoisModel = poisModel;
        self.buyTicketHeaderView.startTextField.text = poisModel.name;
    }else{
        self.endPoisModel = poisModel;
        self.buyTicketHeaderView.endTextField.text = poisModel.name;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    return self.regularBusViewModel.resultList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightFoFooterInSection:(NSInteger)section{
    return 0.01;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (self.regularBusViewModel.resultList.count > 0) {
       return self.recommendSectionView;
    }else{
        return [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 0, 0)];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BusLineCell *lineCell = [tableView dequeueReusableCellWithIdentifier:[BusLineCell cellIdentifier]];
    if (lineCell == nil) {
        [tableView registerNib:[UINib nibWithNibName:[BusLineCell cellIdentifier] bundle:nil] forCellReuseIdentifier:[BusLineCell cellIdentifier]];
        lineCell = [tableView dequeueReusableCellWithIdentifier:[BusLineCell cellIdentifier]];
    }
    [lineCell showModel:self.regularBusViewModel.resultList[indexPath.row]];
    return lineCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RegularBusLineModel *regularBusLineModel = self.regularBusViewModel.resultList[indexPath.row];
    RoutePlanningViewController *routePlanningViewController = (RoutePlanningViewController *)[RoutePlanningViewController getViewControllerFromSbName:@"RoutePlanning" viewControllerName:@"RoutePlanningViewController"];
    routePlanningViewController.routePlanningStatus = RoutePlanningSuccess;
    routePlanningViewController.lineId = @(regularBusLineModel.LineId).stringValue;
    routePlanningViewController.StartStationId = regularBusLineModel.StartStationId;
    routePlanningViewController.EndStationId  = regularBusLineModel.EndStationId;
    routePlanningViewController.BCID = regularBusLineModel.BCID;
    [self.buyTicketsViewController.navigationController pushViewController:routePlanningViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
